package com.etc.springbootssm.controller;

import com.etc.springbootssm.entity.Disease;
import com.etc.springbootssm.service.DiseaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.RowSet;
import java.util.List;

@RestController
@Api(value = "疾病控制器",description = "疾病控制器")
public class DiseaseController {
    @Autowired
    private DiseaseService service;

    @ApiOperation(httpMethod = "GET",notes = "疾病模糊查询分页",value = "疾病模糊查询分页")
    @GetMapping("/selectbykeywords")
    public PageInfo<Disease> selectByPage(@ApiParam(value = "当前页码") @RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo, @ApiParam(value = "每页记录数") @RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize, @ApiParam(value = "查询的关键字") @RequestParam(required = false,value = "keywords",defaultValue = "") String keywords){

        PageHelper.startPage(pageNo,pageSize);
        List<Disease> diseases = service.selectByPage(keywords);
        System.out.println(diseases);
        PageInfo<Disease> newsPageInfo = new PageInfo<>(diseases);
        return newsPageInfo;
    }

    @DeleteMapping
    @ApiOperation(value = "疾病表", httpMethod = "DELETE", notes = "物理删除Disease字段", response = Integer.class)
    public String deleteDisease(@PathVariable Long DiseaseId) {
        if (service.deleteByPrimaryKey(DiseaseId) == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }



    @PostMapping("/addDisease")
    @ApiOperation(value = "疾病表", httpMethod = "POST", notes = "添加疾病", response = Integer.class)
    public String addDisease(@RequestBody Disease disease) {

        System.out.println(disease);
        if (service.insert(disease)==1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }
    @PutMapping
    @ApiOperation(value = "疾病表", httpMethod = "PUT", notes = "修改Disease字段", response = Integer.class)
    public String updateDisease(@RequestBody Disease disease) {
        if (service.updateByPrimaryKey(disease) == 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }


}
